package com.muli.muwai.api;


import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpClientUtil {

    private static Gson gson = new Gson();

    public static InputStream String2Inputstream(String str) {
        return new ByteArrayInputStream(str.getBytes());
    }

    public Map sendPost(String accessToken, MultiValueMap<String,Object> param, String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        /*headers.add("accessToken",accessToken);*/
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String,Object>> requestParam = new HttpEntity(param,headers);
        String body=restTemplate.postForObject(url, requestParam, String.class);
        Map<String,Object> result = gson.fromJson(body,Map.class);
        return result;
    }

    public Map sendGet(String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String body=restTemplate.getForObject(url, String.class);
        Map<String,Object> result = gson.fromJson(body,Map.class);
        return result;
    }

    public String sendGet2(String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String body=restTemplate.getForObject(url, String.class);
        return body;
    }

    public Map sendPostForJson(String accessToken, String param, String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        if (StringUtils.isNotBlank(accessToken)){
            headers.add("Authorization","Bearer " + accessToken);
        }
        HttpEntity<String> requestParam = new HttpEntity(param,headers);
        String body=restTemplate.postForObject(url, requestParam, String.class);
        Map<String,Object> result = gson.fromJson(body,Map.class);
        return result;
    }

    public String sendPostForCy(String authToken, MultiValueMap<String,Object> param, String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("AuthToken",authToken);
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<Map<String,Object>> requestParam = new HttpEntity(param,headers);
        String body=restTemplate.postForObject(url, requestParam, String.class);
        return body;
    }

    public Map sendPostForShengDa(String param, String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> requestParam = new HttpEntity(param,headers);
        String body=restTemplate.postForObject(url, requestParam, String.class);
        Map<String,Object> result = gson.fromJson(body,Map.class);
        return result;
    }

    public Map sendPostForTopUp(MultiValueMap<String,Object> param, String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<Map<String,Object>> requestParam = new HttpEntity(param,headers);
        String body=restTemplate.postForObject(url, requestParam, String.class);
        Map<String,Object> result = gson.fromJson(body,Map.class);
        return result;
    }

    public Map sendTest(String parmJson, String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String body=restTemplate.postForObject(url, parmJson, String.class);
        System.out.println(body);
        Map<String,Object> result = gson.fromJson(body,Map.class);
        return result;
    }


    public  String httpPostWithjson(String url, String json) {
        String result = "";
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            BasicResponseHandler handler = new BasicResponseHandler();
            StringEntity entity = new StringEntity(json, "utf-8");//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            result = httpClient.execute(httpPost, handler);
            return result;
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os=httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

